
function getCache(prop) {
    return JSON.parse(localStorage.getItem(prop))
}

function reloadQuiz(tab) {
    chrome.runtime.sendMessage({msg:'reload'})
}

function nextQuiz() {
    chrome.runtime.sendMessage({msg:'next'})
}

function sendMessageToTab(tabid,message) {
    chrome.tabs.sendMessage(tabid,message)
}

chrome.runtime.onMessage.addListener((message,sender)=>{
    const prop = 'quizzes'
    const quizzes = getCache('quizzes')||[]
    const tab = sender.tab
    switch (message.msg) {
        case 'quiz_info':
            if(message.data){
                let data = message.data;
                
                const data_filter = data.filter(item=>{
                    if(item.url.indexOf('?')!=-1){
                        item.url = item.url.substring(0,item.url.indexOf('?'))
                    }
                    for(let i=0;i<quizzes.length;i++){
                        if(quizzes[i].url === item.url||quizzes[i].title === item.title){
                            quizzes[i].thumbs = item.thumbs;
                            console.log("url重复")
                            return false
                        }
                        continue;
                    }
                    return true;                    
                })
                data = quizzes.concat(data_filter)
                console.log(data,'重新获取的数据')
                // 本地缓存数据
                localStorage.setItem(prop,JSON.stringify(data))
            }            
            break;
        case 'quiz_result':  
            if(message.data){
                const url = sender.url.replace('/results/','/test/'); // 获取message来源
                for(let i =0;i<quizzes.length;i++){
                    const quiz = quizzes[i];
                    if(url.indexOf(quiz.url)!=-1){  // 找到对应的quiz
                        let results = quiz.results || [];
                        if(!results.includes(url)){
                            results.push(message.data)
                            quiz.results = results
                            const tab = sender.tab;
                            const select = quiz.select
                            console.log('收到results',quiz)
                            if(select&&select.options&&select.options.length){  // 选择题
                                if(select.options.length==quiz.results.length){
                                    quiz.done = true
                                    nextQuiz(tab)
                                }else{
                                    reloadQuiz(tab)
                                }
                            }else{  // 普通quiz
                                if(quiz.results.length<6){
                                    reloadQuiz(tab)
                                }else{
                                    quiz.done = true
                                    nextQuiz(tab)
                                }
                            }

                            localStorage.setItem(prop,JSON.stringify(quizzes))
                        }
                        break;
                    }
                }  
            }
            break;
        case 'quiz_desc':
            if(message.data){
                const data = message.data;
                for(let i=0;i<quizzes.length;i++){
                    const quiz = quizzes[i]
                    if(quiz.url === sender.url){
                        quiz.desc = data.desc
                        if(data.select){    // 存储select
                            quiz.select = data.select
                            sendMessageToTab(tab.id,{msg:'quiz_options',data:quiz}) // 开始采集每个option的结果
                        }
                        localStorage.setItem(prop,JSON.stringify(quizzes))
                        break;
                    }
                }
            }
            break;
        case 'quiz_error':
            for(let i=0;i<quizzes.length;i++){
                const quiz = quizzes[i]
                if(quiz.url === sender.url){
                    quizzes.splice(i,1) // 移除发生错误的quiz
                    localStorage.setItem(prop,JSON.stringify(quizzes))
                    console.log('采集出错，删除该quiz',quiz)
                    break;
                }
            }
            nextQuiz();
            break;
        default:
            break;
    }

})